﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XcoAppSpaces.Contracts.Security;
using Microsoft.Ccr.Core;
using XcoAppSpaces.Contracts.Exceptions;
using XcoAppSpaces.Contracts.Service;
using XcoAppSpaces.Contracts.Logging;
using System.Net;

namespace XcoAppSpaces.Security.Basic
{
	/// <summary>
	/// Security Service that just permits all actions.
	/// </summary>
	public class XcoNoSecurityService : IXcoSecurityService
	{

		#region IXcoSecurityService Members

		/// <summary>
		/// Authenticates the given user and address. Allows all users as authenticated and returns an empty session id.
		/// </summary>
		/// <param name="address"></param>
		/// <param name="credentials"></param>
		/// <returns></returns>
		public XcoAuthenticationResult Authenticate(string address, IXcoClientCredentials credentials)
		{
			return new XcoAuthenticationResult { IsAuthenticated = true, SessionID = Guid.Empty };
		}

		/// <summary>
		/// Authorizes a given user to access a given worker. Always returns true.
		/// </summary>
		/// <param name="address"></param>
		/// <param name="sessionID"></param>
		/// <param name="worker"></param>
		/// <returns></returns>
		public bool Authorize(string address, Guid sessionID, IPort worker)
		{
			return true;
		}

		/// <summary>
		/// Checks for permission to access the worker port for the given message type. Permission is always granted,
		/// a SecurityException is never thrown.
		/// </summary>
		/// <param name="worker"></param>
		/// <param name="message"></param>
		/// <param name="address"></param>
		/// <param name="sessionID"></param>
		public void CheckPermission(IPort worker, object message, string address, Guid sessionID)
		{
		}

		/// <summary>
		/// Creates an XcoBasicClientCredentials object for the given client credentials. This works
		/// the same way as in the XcoBasicSecurityService, so that XcoAppSpace instances with no
		/// security can communicate with spaces using the XcoBasicSecurityService without any special
		/// configuration.
		/// </summary>
		/// <param name="credentials">The ICredentials that have been set for the local space instance.</param>
		/// <param name="address">The address for which a user is needed.</param>
		/// <returns>An XcoBasicClientCredentials instance according to the given credentials.</returns>
		public IXcoClientCredentials GetClientCredentials(ICredentials credentials, string address)
		{
			//Generate XcoBasicClientCredentials for compatibility with the XcoBasicSecurityService
			return credentials.GetXcoClientCredentials(address);
		}

		/// <summary>
		/// Gets the username for the given session id.
		/// </summary>
		/// <param name="sessionID">The id of the session for which the user should be retrieved.</param>
		/// <returns>The username for the given session, or null if no user is applied to this session.</returns>
		public string GetUsername(Guid sessionID)
		{
			return null;
		}

		#endregion

		#region IXcoStatefulService Members

		/// <summary>
		/// Starts the service.
		/// </summary>
		public void Start()
		{
		}

		/// <summary>
		/// The start priority of the service.
		/// </summary>
		public int StartPriority
		{
			get { return 3; }
		}

		/// <summary>
		/// Stops the service.
		/// </summary>
		public void Stop()
		{
		}

		#endregion

		#region IXcoService Members

		/// <summary>
		/// Initializes the service.
		/// </summary>
		/// <param name="serviceRegistry"></param>
		public void Initialize(IXcoServiceRegistry serviceRegistry)
		{
		}

		#endregion
	}
}
